﻿using System;

namespace TestWebApplication.CommerceTestDataProvider
{
    /// <summary>
    /// Represents a Passport User ID, which contains a high and a low value.
    /// Puid objects are immutable, and can never be changed.
    /// </summary>
    [Serializable]
    public struct Puid
    {
        /// <summary>
        /// The empty Puid, with both its high and low value set to 0
        /// </summary>
        public static readonly Puid Empty;

        private int high;
        private int low;

        static Puid()
        {
            Puid.Empty = new Puid(0, 0);
        }

        /// <summary>
        /// Creates a new Puid object
        /// </summary>
        /// <param name="high">The high value</param>
        /// <param name="low">The low value</param>
        public Puid(int high, int low)
        {
            this.high = high;
            this.low = low;
        }

        /// <summary>
        /// Creates a new Puid object
        /// </summary>
        /// <param name="puid">The puid to copy</param>
        public Puid(Puid puid)
        {
            this.high = puid.High;
            this.low = puid.Low;
        }

        /// <summary>
        /// Gets / Sets the high value for this Puid
        /// </summary>
        public int High
        {
            get
            {
                return this.high;
            }
            set
            {
                this.high = value;
            }
        }

        /// <summary>
        /// Gets / Sets the low value for this Puid
        /// </summary>
        public int Low
        {
            get
            {
                return this.low;
            }
            set
            {
                this.low = value;
            }
        }

        /// <summary>
        /// Generates a random Puid
        /// </summary>
        /// <returns>A new, randomly generated Puid</returns>
        public static Puid RandomPuid()
        {
            int high = GlobalRandom.Next(0, Int32.MaxValue);
            int low = GlobalRandom.Next(Int32.MinValue, Int32.MaxValue);
            return new Puid(high, low);
        }

        /// <summary>
        /// Converts this Puid into a long integer.
        /// </summary>
        /// <returns>A long integer version of this Puid</returns>
        public ulong ToLong()
        {
            return (ulong)((((Int64)High) << 32) + ((UInt32)Low));
        }

        /// <summary>
        /// Converts this Puid into a long integer.
        /// </summary>
        /// <returns>A long integer version of this Puid</returns>
        public override string ToString()
        {
            return this.ToLong().ToString();
        }
    }
}
